<template>
  <div class="container">
    <!-- 路径导航区域 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>师资管理</el-breadcrumb-item>
      <el-breadcrumb-item>咨询师管理</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 搜索区域 -->
    <el-row>
      <el-form :inline="true">
        <el-form-item label="咨询师姓名">
          <el-input
            v-model="queryInfo.consultName"
            clearable
            placeholder="请输入咨询师姓名"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="queryList">搜索</el-button>
          <el-button type="primary" @click="toAddConsult">添加</el-button>
        </el-form-item>
      </el-form>
    </el-row>
    <!-- 表格区域 -->
    <el-table :data="ConsultList" border style="width: 100%">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column prop="consultId" label="ID"></el-table-column>
      <el-table-column prop="consultName" label="名称"> </el-table-column>
      <el-table-column prop="consultSex" label="性别"> </el-table-column>
      <el-table-column prop="consultLv.consultLvName" label="等级">
      </el-table-column>
      <el-table-column prop="consultPicture" label="头像">
        <template slot-scope="scope">
          <!--  预览使用动态的url实现拼接 -->
          <el-image
            style="width: 100px; height: 100px"
            :src="url + 'download?filename=' + scope.row.consultPicture"
          ></el-image>
        </template>
      </el-table-column>

      <el-table-column prop="field.fieldName" label="擅长领域"></el-table-column>
      <el-table-column prop="consultAddress" label="地址"> </el-table-column>
      <el-table-column prop="consultIntro" label="简介"> </el-table-column>
      <el-table-column label="是否认证">
        <template slot-scope="scope">
          <el-link type="primary" v-if="scope.row.consultAuthe == 1"
            >已认证</el-link
          >
          <el-link type="primary" v-if="scope.row.consultAuthe == 2"
            >未认证</el-link
          >
        </template>
      </el-table-column>
      <el-table-column prop="consultCert" label="证书图片"> </el-table-column>

      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-link type="primary" @click="toEditConsult(scope.row)"
            >编辑</el-link
          >
          <el-link type="primary" @click="deleteConsult(scope.row.consultId)"
            >删除</el-link
          >
        </template>
      </el-table-column>
    </el-table>
    <!-- 分页区域 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="queryInfo.pageNum"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="queryInfo.pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total"
    >
    </el-pagination>

    <!-- 新增和修改的对话框 -->
    <el-dialog :title="title" :visible.sync="dialogFormVisible" @close="cancle">
      <el-form :model="consult">
        <el-form-item label="姓名" label-width="80px">
          <el-input
            placeholder="请输入咨询师姓名"
            v-model="consult.consultName"
          ></el-input>
        </el-form-item>
        <el-form-item label="性别" label-width="80px">
          <el-radio v-model="consult.consultSex" label="男">男</el-radio>
          <el-radio v-model="consult.consultSex" label="女">女</el-radio>
        </el-form-item>
        <el-form-item label="等级" label-width="80px">
          <el-select
            v-model="consult.consultLvId"
            filterable
            placeholder="请选择咨询师等级"
          >
            <el-option
              v-for="item in consultLvList"
              :key="item.consultLvId"
              :label="item.consultLvName"
              :value="item.consultLvId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="头像" label-width="80px">
          <el-upload
            :action="url + 'upload'"
            list-type="picture-card"
            :on-success="handleSuccess"
            :file-list="fileList"
            :limit="1"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove"
          >
            <!-- 加号图标 -->
            <i class="el-icon-plus"></i>
          </el-upload>
          <!-- 预览图片 -->
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="" />
          </el-dialog>
        </el-form-item>

        <el-form-item label="擅长领域" label-width="80px">
          <el-select
            v-model="consult.consultFieldId"
            filterable
            placeholder="请选择擅长领域"
          >
            <el-option
              v-for="item in FieldList"
              :key="item.fieldId"
              :label="item.fieldName"
              :value="item.fieldId"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="地址" label-width="80px">
          <el-input
            placeholder="请输入地址"
            v-model="consult.consultAddress"
          ></el-input>
        </el-form-item>
        <el-form-item label="简介" label-width="80px">
          <div style="margin: 20px 0"></div>
          <el-input
            type="textarea"
            placeholder="请输入内容"
            v-model="consult.consultIntro"
            maxlength="50"
            show-word-limit
          >
          </el-input>
        </el-form-item>
        <el-form-item label="是否认证" label-width="80px">
          <el-radio v-model="consult.consultAuthe" label="1">已认证</el-radio>
          <el-radio v-model="consult.consultAuthe" label="2">未认证</el-radio>
        </el-form-item>
        <el-form-item label="证书图片" label-width="80px">
          <el-input
            placeholder="请选择证书图片"
            v-model="consult.consultCert"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancle">取 消</el-button>
        <el-button type="primary" @click="addConsult">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { url } from "@/util/tool.js";

export default {
  data() {
    return {
      // 查询信息对象
      queryInfo: {
        consultName: "",
        pageNum: 1,
        pageSize: 5,
      },
      //咨询师对象
      consult: {
        consultId: "",
        consultName: "",
        consultSex: "",
        consultLvId: "",
        consultPicture: "",
        consultFieldId: "",
        consultAddress: "",
        consultIntro: "",
        consultAuthe: "",
        consultCert: "",
      },

      total: 183,
      ConsultList: [],
      dialogFormVisible: false,
      title: "",
      consultLvs: [],
      consultLvList: [],
      Fields: [],
      FieldList: [],
      dialogImageUrl: "", // 预览图片路径
      dialogVisible: false, // 预览图片的对话框
      fileList: [], //文件列表
      url: "",
    };
  },

  methods: {
    // 处理上传成功的函数
    handleSuccess(response) {
      // 把上传后的图片路径，交给文章对象的consultPicture
      this.consult.consultPicture = response.message;
    },

    // 删除图片的事件
    handleRemove(file, fileList) {
      console.log(file, fileList);
      // 移除数据
      this.consult.consultPicture = "";
    },

    // 预览图片事件
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },

    //查询所有的咨询师等级信息
    queryConsultLv() {
      this.$http.get("consultLv/findAllConsultLv").then((res) => {
        //把结果数据赋值给 chargeTypeList
        this.consultLvList = res.data.data;
        //处理
        for (var i = 0; i < res.data.data.length; i++) {
          this.$set(
            this.consultLvs,
            res.data.data[i].consultLvId,
            res.data.data[i].consultLvName
          );
        }
        console.log(this.consultLvs);
      });
    },

    //查询所有的擅长领域信息
    queryField() {
      this.$http.get("field/findAllField").then((res) => {
        //把结果数据赋值给 FieldList
        this.FieldList = res.data.data;
        //处理
        for (var i = 0; i < res.data.data.length; i++) {
          this.$set(
            this.Fields,
            res.data.data[i].fieldId,
            res.data.data[i].fieldName
          );
        }
        console.log(this.Fields);
      });
    },

    //点击修改打开对话框
    toEditConsult(row) {
      this.consult = row;
      this.title = "修改信息";
      this.dialogFormVisible = true;
      // 动态的获取图片的名称并绑定到fileList，使用全局url实现拼接
      this.fileList = [
        {
          name: row.consultPicture,
          url: this.url + "download?filename=" + row.consultPicture,
        },
      ];
    },
    //点击添加打开对话框
    toAddConsult() {
      this.title = "添加信息";
      this.dialogFormVisible = true;
    },
    // 添加或修改功能
    addConsult() {
      if (this.consult.consultId) {
        //修改
        this.$http.put("consult/editConsult", this.consult).then((res) => {
          if (res.data.status == 200) {
            this.$message.success("修改成功！");
            //清除数据
            this.consult = {
              consultId: "",
              consultName: "",
              consultLvId: "",
              consultPicture: "",
              consultFieldId: "",
              consultAddress: "",
              consultIntro: "",
              consultAuthe: "",
              consultCert: "",
            };
            //关闭对话框
            this.dialogFormVisible = false;
            this.fileList = [];
            //重新查询
            this.queryList();
          } else {
            this.$message.error("修改失败！");
          }
        });
      } else {
        //添加
        this.$http.post("consult/addConsult", this.consult).then((res) => {
          if (res.data.status == 200) {
            this.$message.success("添加成功！");
            //清除数据
            this.consult = {
              consultId: "",
              consultName: "",
              consultLvId: "",
              consultPicture: "",
              consultFieldId: "",
              consultAddress: "",
              consultIntro: "",
              consultAuthe: "",
              consultCert: "",
            };
            //关闭对话框
            this.dialogFormVisible = false;
            this.fileList = [];
            //重新查询
            this.queryList();
          } else {
            this.$message.error("添加失败！");
          }
        });
      }
    },

    // 删除
    deleteConsult(id) {
      this.$confirm("此操作将永久删除该文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http.delete("consult/removeConsult/" + id).then((resp) => {
            if (resp.data.status == 200) {
              this.$message.success(resp.data.message);
              this.queryList();
            } else {
              this.$message.error(resp.data.message);
            }
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消删除",
          });
        });
    },

    //取消事件
    cancle() {
      this.dialogFormVisible = false;
      this.consult = {
        consultName: "",
        consultLvId: "",
        consultPicture: "",
        consultFieldId: "",
        consultAddress: "",
        consultIntro: "",
        consultAuthe: "",
        consultCert: "",
      };
      this.fileList = [];
    },

    //分页查询的方法
    queryList() {
      //调用后端接口获取数据
      this.$http
        .get("consult/findByPageConsult", {
          params: this.queryInfo,
        })
        .then((res) => {
          if (res.data.status == 200) {
            console.log(res.data);
            this.ConsultList = res.data.data.list;
            this.total = res.data.data.total;
          } else {
            this.$message.error("查询失败！");
          }
        });
    },

    handleSizeChange(size) {
      //   console.log("每页显示数量发生改变");
      this.queryInfo.pageSize = size;
      this.queryInfo.pageNum = 1;
      //重新查询
      this.queryList();
    },
    handleCurrentChange(num) {
      //   console.log("页码发生改变");
      this.queryInfo.pageNum = num;
      //重新查询
      this.queryList();
    },
  },

  // 页面加载时就查询数据
  mounted() {
    this.queryList();
    this.queryConsultLv();
    this.queryField();
    // 动态的获取tool.js中的url，并赋值给当前组件的全局url
    this.url = url;
  },
};
</script>

<style>
.el-breadcrumb {
  margin-bottom: 15px;
  padding: 0 0 15px;
  border-bottom: 1px solid #ddd;
}

.el-pagination {
  margin-top: 15px;
}

.el-link {
  padding: 0 5px;
}
</style>